<template>
	<view class="uni-tab-bar">
		<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex==index ? 'active' : '']" :id="tab.id" :data-current="index" @tap="tapTab">{{tab.name}}</view>
		</scroll-view>
		
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab" :style="{'height': systemInfo +'px'}">
			<swiper-item v-for="(tab,index1) in newsitems" :key="index1">
				<scroll-view class="list" scroll-y @scrolltolower="loadMore">
					
					<block><!-- v-for="(newsitem,index2) in tab.data" :key="index2" -->
						订单详情区域(一般这个区域移交自定义组件)
						<!-- <media-list :data="newsitem"  @cancel="cancel(newsitem.order_number)" @confirm='confirm(newsitem.order_number)' @log="log(newsitem.order_number)" @evaluate="evaluate(newsitem.id)" @del="del(newsitem.id)" @pay="pay(newsitem.order_number,newsitem.pay_type)" @click="goDetail(newsitem.id)"></media-list> -->
					</block>
					
				</scroll-view>
			</swiper-item>
		</swiper>

	</view>
</template>

<script>
	// import mediaList from '@/components/tab-nvue/mediaList.vue';
	export default {
		// components: {
		// 	mediaList
		// },
		data() {
			return {
				user_id:'',
				res:[],
				systemInfo:[],
				tabIndex: 0, // 导航栏当前点击项
				scrollLeft: 0,
				isClickChange: false,
				newsitems: [
					{
						"loadingType":0,
						"data":[], // 普通订单
					},{
						"loadingType":0,
						"data":[],
					},{
						"loadingType":0,
						"data":[],
					},{
						"loadingType":0,
						"data":[],
					},{
						"loadingType":0,
						"data":[],
					},
					{
						"loadingType":0,
						"data":[],
					}
				],
				tabBars: [
					{
						name: '全部',
						id: 'all'
					}, {
						name: '待付款',
						id: 'pending_pay'
					}, {
						name: '待发货',
						id: 'to_be_delivered'
					}, {
						name: '已发货',
						id: 'shipped'
					}, {
						name: '已完成',
						id: 'finished'
					}, {
						name: '已取消',
						id: 'cancled'
					}
				]
			}
		},
		onLoad: function(e) {
			this.tabIndex = 1
			setTimeout(() => {
				this.tabIndex = e.index
			}, 1000);
			uni.getSystemInfo({
				success: (res) => {
					this.systemInfo = res.windowHeight -50
				}
			})
		},
		methods: {
			// tab切换功能
			async changeTab(e) {
				let index = e.target.current;
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			// tab栏点击
			async tapTab(e) {
				if (this.tabIndex == e.target.dataset.current) {
					return false;
				} else {
					this.isClickChange = true;
					this.tabIndex = e.target.dataset.current
				}
			},
		}
	}
</script>

<style lang="scss">
	uni-page-body{height:100%;}
	.uni-swiper-tab {
		width: 100%;
		white-space: nowrap;
		line-height: 110upx;
		height: 120upx;
		border-bottom: 1px solid #c8c7cc;
		background: #fff;
	}
	.swiper-tab-list {
		font-size: 30upx;
		width: 150upx;
		display: inline-block;
		text-align: center;
		color: #555;
	}
	// 选中状态字体颜色
	.active {
		color: #007AFF;
	}
</style>
